package com.orangelife.mobile.parkingcard.biz;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2016/7/6.
 */
public class ParkingCardBiz {

    public static boolean validate(Context context, String name, String tel,String timeStampStart,String timeStampEnd, String linkAptHouseholderID) {
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(context, "名字不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(tel)) {
            Toast.makeText(context, "电话不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(linkAptHouseholderID)) {
            Toast.makeText(context, "认证ID不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if(Long.parseLong(timeStampStart)>Long.parseLong(timeStampEnd)){
            Toast.makeText(context,"起始时间不能大于截止时间",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(Long.parseLong(timeStampStart)<getTimesmorning()||Long.parseLong(timeStampEnd)<getTimesmorning()){
            Toast.makeText(context,"起始时间、截止时间均不得早于当天",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(Long.parseLong(timeStampStart)>Long.parseLong(timeStampEnd)){
            Toast.makeText(context,"起始时间不能大于截止时间",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    //获得当天0点时间
    public static long getTimesmorning(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        long time=cal.getTimeInMillis();
        return  time;
    }

    public static String addZero(int s) {
        String s1 = String.valueOf(s);
        if (s1 != null && s1.length() == 1) {
            return "0" + s1;
        }
        return s1;
    }

    public static long parseDate(String formatDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(formatDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date == null ? 0 : date.getTime();
    }
}
